<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $sector_max;

$message .= 'Towing bigger players out of restricted zones...';


$result = $smcFunc['db_query']('', '
	SELECT {raw:db_ships}.ship_id, {raw:db_ships}.character_name, {raw:db_ships}.hull, {raw:db_ships}.sector, {raw:db_universe}.zone_id, max_hull
	FROM {raw:db_universe}, {raw:db_ships}, {raw:db_zones}
	WHERE sector = sector_id
	AND {raw:db_universe}.zone_id = {raw:db_zones}.zone_id
	AND max_hull <> 0
	AND (({raw:db_ships}.hull + {raw:db_ships}.engines + {raw:db_ships}.computer + {raw:db_ships}.beams + {raw:db_ships}.torp_launchers + {raw:db_ships}.shields + {raw:db_ships}.armor) / 7) > max_hull
	AND ship_destroyed = "N"',
	array(
		'db_universe' => $dbtables['universe'],
		'db_ships' => $dbtables['ships'],
		'db_zones' => $dbtables['zones']
	)
);

if ($smcFunc['db_num_rows']($result) > 0)
{
	$message .= '<br>' . $smcFunc['db_num_rows']($result) . ' players to tow:<br>';
	
	while ($row = $smcFunc['db_fetch_assoc']($result))
	{
		$message .= '...towing ' . $row['character_name'] . ' out of ' . $row['sector'] . ' ...';
		$newsector = rand(0, $sector_max - 1);
		$message .= ' to sector ' . $newsector . '<br>';
		
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET sector = {int:newsector},
				cleared_defences = NULL
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'newsector' => $newsector,
				'ship_id' => $row['ship_id']
			)
		);
		playerlog($row['ship_id'], LOG_TOW, "$row[sector]|$newsector|$row[max_hull]");
		log_move($row['ship_id'],$newsector);
	}
}
else
	$message .= '<br>No players to tow.<br>';

$smcFunc['db_free_result']($result);

  
$message .= '<br>';

?>
